package com.neuedu.wemall.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.wemall.common.vo.ResponseData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import com.neuedu.wemall.dao.entity.Favorite;
import com.neuedu.wemall.service.IFavoriteService;

import java.util.Date;

/**
 * Author lu
 * Date   2025-09-08 15:17:25
 */
@Log4j2
@RequestMapping("/wemall/member/collect/api")
@RestController
@Tag(name = "会用商品收藏控制器")
public class FavoriteController extends BaseController {

    @Autowired
    @Qualifier("favoriteService")
    private IFavoriteService favoriteService;

    /**
     * 将商品添加到收藏夹
     *
     * @param prodId 待收藏商品ID
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "将商品添加到收藏夹")
    @Parameters({
            @Parameter(name = "prodId", description = "收藏商品ID", required = true),
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData addFavorite(@RequestParam(name = "prodId", required = true) int prodId) {
        int userId = getMemberUserID();
        LambdaQueryWrapper<Favorite> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Favorite::getUserId, userId);
        lqw.eq(Favorite::getProdId, prodId);
        Favorite favorite = favoriteService.getOne(lqw);
        if (favorite != null) {
            return ResponseData.createFail("该商品已收藏");
        }
        favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setProdId(prodId);
        favorite.setCreateTime(new Date());
        boolean flag = favoriteService.save(favorite);
        return ResponseData.success(flag);
    }


    /**
     * 删除收藏夹商品
     *
     * @param prodId 待删除收藏商品ID
     * @return
     */
    @DeleteMapping("/del")
    @Operation(summary = "删除收藏夹商品")
    @Parameters({
            @Parameter(name = "prodId", description = "收藏商品ID", required = true),
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData delFavorite(@RequestParam(name = "prodId", required = true) int prodId) {
        int userId = getMemberUserID();
        LambdaQueryWrapper<Favorite> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Favorite::getUserId, userId);
        lqw.eq(Favorite::getProdId, prodId);
        Favorite favorite = favoriteService.getOne(lqw);
        if (favorite == null) {
            return ResponseData.createFail("该商品未收藏");
        }
        boolean flag = favoriteService.remove(lqw);
        return ResponseData.success(flag);
    }
    /**
     * 是否收藏该商品
     *
     * @param prodId 商品ID
     * @return
     */
    @GetMapping("/contains")
    @Operation(summary = "是否收藏该商品")
    @Parameters({
            @Parameter(name = "prodId", description = "收藏商品ID", required = true),
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData contains(@RequestParam(name = "prodId", required = true) int prodId) {
        int userId = getMemberUserID();
        LambdaQueryWrapper<Favorite> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Favorite::getUserId, userId);
        lqw.eq(Favorite::getProdId, prodId);
        long count = favoriteService.count(lqw);
        return ResponseData.success(count);
    }

    /**
     * 分页查询收藏夹商品
     *
     * @param pageNum  页码
     * @param pageSize 每页记录数
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询收藏夹商品")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = true),
            @Parameter(name = "pageSize", description = "每页记录数", required = true),
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData listFavorite(@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
                                     @RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize) {
        int userId = getMemberUserID();
        Page<Favorite> page = favoriteService.selectFavoriteList(pageNum, pageSize, userId);
        return ResponseData.success(page);
    }
}